/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import abstractmodel.IDetail;
import abstractmodel.IInfo;
import abstractmodel.StatisticsType;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fightfxj
 */
public class CreditCardAccount extends Account {

    private CreditCardAccountInfo accountInfo;

    public CreditCardAccount() {
        super();
        accountInfo = new CreditCardAccountInfo();
    }
  
    @Override
    public IInfo getAccountInfo() {
        return accountInfo;
    }

    @Override
    public boolean setAccountInfo(IInfo info) {
        accountInfo = (CreditCardAccountInfo) info;
        return true;
    }

    @Override
    public boolean deleteDetailInfo(IDetail detail) {
        if (ModelFactory.data.deleteDetailInfo(detail.getDetailInfo()))
            return true;
        else
            return false;
    }

    @Override
    public ArrayList<IDetail> getDetailsInfo() {
       ArrayList<abstractmodel.IInfo> infoList = ModelFactory.data.search("parentID==" + this.accountInfo.getid(), IInfo.class);
       ArrayList<IDetail> detailList = new ArrayList<IDetail>();
       for (int i = 0; i < infoList.size(); i++) {
           Detail d = new Detail();
           d.detailInfo = (CreditCardDetailInfo)infoList.get(i);
           detailList.add(d);
       }
       return detailList;
    }

    @Override
    public StatisticsInfo getStatistics(StatisticsType type) {
        ArrayList<IDetail> detailList = this.searchDetailInfo("id>=0");
        java.util.Collections.sort((List)detailList, new model.DetailTimeComparator());
        StatisticsInfo sinfo = new StatisticsInfo();
        for (IDetail d : detailList) {
            sinfo.add(d.getDetailInfo().getValue("收支日期").getString(), new Double(d.getDetailInfo().getValue("数额").getDouble()));
        }
        sinfo.setType(type);        
        return sinfo;
    }

    @Override
    public ArrayList<IDetail> searchDetailBetweenDate(String timeBeginString, String timeEndString) {
        ArrayList<IDetail> detailListAll = this.searchDetailInfo("id>=0");
        ArrayList<IDetail> detailListWant = new ArrayList<IDetail>();
        for (IDetail d : detailListAll) {
            try {
                String dTimeString = d.getDetailInfo().getValue("收支日期").getString();

                Date dateBegin = configure.Global.sdf.parse(timeBeginString);
                Date dateEnd = configure.Global.sdf.parse(timeEndString);
                Date dTime = configure.Global.sdf.parse(dTimeString);
                if (dTime.compareTo(dateBegin) >= 0 && dTime.compareTo(dateEnd) <= 0) {
                    detailListWant.add(d);
                }
                
            } catch (ParseException ex) {
                Logger.getLogger(BankAccount.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return detailListWant;
    }    
    
    @Override
    public ArrayList<IDetail> searchDetailInfo(String filter) {
       ArrayList<abstractmodel.IInfo> infoList = ModelFactory.data.search(filter+"&& parentID==" + this.accountInfo.getid(), IInfo.class);
       ArrayList<IDetail> detailList = new ArrayList<IDetail>();
       for (int i = 0; i < infoList.size(); i++) {
           Detail d = new Detail();
           d.detailInfo = (CreditCardDetailInfo)infoList.get(i);
           detailList.add(d);
       }
       return detailList;
    }

    @Override
    public double getTotalLeftMoney() {
        ArrayList<IDetail> detailList = this.searchDetailInfo("id>=0");
        //java.util.Collections.sort((List)detailList, new model.DetailTimeComparator());
        double count = 0;
        for (IDetail d : detailList) {
            count += d.getDetailInfo().getValue("数额").getDouble();
        }
        return count;        
    }
    /**
     * 打印帐户的所有相关信息，供调试用
     */
    public void printAllAccountInfo() {
        //titles = new String[]{"帐户名称", "归属银行", "卡号", "开户名", "额度", "有效日期", "还款日期", "帐单日"};
         System.out.println("Titles : ");
        for (int i = 0;i < accountInfo.getTitles().length; i ++)
            System.out.print(accountInfo.getTitles()[i] +  " ");
        System.out.println();         
        System.out.println("帐户名称：" + accountInfo.getValue("帐户名称").getString());
        System.out.println("归属银行：" + accountInfo.getValue("归属银行").getString());
        System.out.println("卡号：" + accountInfo.getValue("卡号").getString());
        System.out.println("开户名：" + accountInfo.getValue("开户名").getString());
        System.out.println("额度：" + accountInfo.getValue("额度").getString());
        System.out.println("有效日期：" + accountInfo.getValue("有效日期").getString());
        System.out.println("还款日期：" + accountInfo.getValue("还款日期").getString());
        System.out.println("帐单日：" + accountInfo.getValue("帐单日").getString());
        System.out.println("ID：" + accountInfo.getValue("id").getString());
    
    }  
}
